/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import bllInterface.Account;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 *
 * @author Mrkupi
 */
public class ClientFriendListModel extends DefaultListModel
{
    private ArrayList<Account> backupAccs;
    private ArrayList<Account> accs;
    
    public ClientFriendListModel()
    {
        accs            =   new ArrayList();
        backupAccs      =   new ArrayList();
    }

    @Override
    public int getSize() 
    {
        return accs.size();
    }

    @Override
    public Object getElementAt(int index) {
        return accs.get(index);
    }
    
    @Override
    public void clear()
    {
        this.accs.clear();
    }
    
    public void clearHistory()
    {
        this.accs.clear();
        this.backupAccs.clear();
    }
    
    public void addAccount( Account acc )
    {
        this.accs.add(acc);
        this.backupAccs.add(acc);
        this.fireIntervalAdded(this, this.accs.size() - 1, this.accs.size());
    }
    
    public Account getAcc( String name )
    {
        for (Account acc : backupAccs)
        {
            try {
                if( acc.getUserName().equals(name) )
                {
                    return acc;
                }
            } catch (RemoteException ex) {
                Logger.getLogger(ClientFriendListModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
    
    public void addAccountWithoutBackup( Account acc )
    {
        this.accs.add(acc);
        this.fireIntervalAdded(this, this.accs.size() - 1, this.accs.size());
    }
    
    public void search( String name ) throws RemoteException
    {
        if( "".equals(name) )
        {
            accs.clear();
            for(Account acc : backupAccs)
            {
                addAccountWithoutBackup(acc);
            }
        }
        else 
        {
            accs.clear();
            ArrayList<Account> copyAcc = new ArrayList<>();
            for(Account acc : backupAccs)
            {
                if(acc.getUserName().contains(name))
                {
                    addAccountWithoutBackup(acc);
                }
            }
        }
        
    }
}
